<div class="app-content-body ">    
    <div class="bg-light lter b-b wrapper-md hidden-print">
      <h1 class="m-n font-thin h3">我的账号</h1>
    </div>
    <form id="password-form">
      <div class="form_title">
        <div class="my_hd">
          更改密码
        </div>
        <div class="crud-container crud-reset">
        	<div class="form-container"></div>
        	<div class="text-right" style="margin-top: 20px;">
        		<div id="save"></div>
        	</div>
        </div>
      </div>
    </form>
</div>
<script>
	$(function(){

		var formInstance = $(".form-container").dxForm({
			readOnly: false,
			validationGroup: "password",
			items: [{
	            dataField: "old_password",
	            label: {text: '原密码'},
	            editorOptions: {
                    mode: "password"
                },
	            validationRules: [{
	                type: "required",
	                message: "请输入原密码"
	            }]
	        },{
	            dataField: "password",
	            label: {text: '新密码'},
	            editorOptions: {
                    mode: "password"
                },
	            validationRules: [{
	                type: "required",
	                message: "请输入会员新密码"
	            }]
	        },{
	        	dataField: "password_confirmation",
                label: {text: "确认新密码"},
                editorType: "dxTextBox",
                editorOptions: {
                    mode: "password"
                },
                validationRules: [{
                    type: "required",
                    message: "请输入确认新密码"
                },{
                	type:"compare",
                	message:'两次输入的密码不一致',
                	comparisonTarget:function(){
                		return formInstance.option("formData").password
                	}
                }]
			}]
		}).dxForm("instance");
		
		$("#password-form #save").dxButton({
	        text: "确认更改",
	        type: "success",
	        useSubmitBehavior: true,
	        validationGroup: "password"
	    });


	    $("#password-form").submit(function () {
	    	// var user = App.getUser();
            var user=formInstance.option('formData')
	    	if (user) {
	    		$.ajax({
	    			url:$.config('apiUrl') + 'auth/change-password',
	    			data:user,
	    			headers: {
				Authorization: "bearer " + localStorage.accessToken
			},
				method:'post',
				success:function(res){
					 			DevExpress.ui.notify({
						message: "已更改，即将跳转登录页",
					}, "success", 1000);
					 formInstance.resetValues();
					 setTimeout(function(){window.location.href = 'login.html';},1500)
					
				},
				error:function(XMLHttpRequest, textStatus, errorThrown){
					console.log(XMLHttpRequest);
						DevExpress.ui.notify({
						message: XMLHttpRequest.responseJSON.message,
					}, "warning", 1000);
				}
	    		})
	    // 		var store = $.crudStore($.config('apiUrl') + 'change-password');
	    // 		store.update(user.id, $.extend({}, user, formInstance.option('formData'))).then(function (resp) {
	    		
	    // 			DevExpress.ui.notify({
					// 	message: "已更改",
					// }, "success", 1000);

					// formInstance.resetValues();
	    // 		});
	    	}
	    	return false;
	    });
	})
</script>